package net.ck.hzq;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import Bean.GetBankcardParamBean;
import Bean.ReturnofincomeBean;
import Utils.HelloWordModel;
import Utils.StringUtil;
import Utils.SystemDatas;
import model.GridviewBean;
import retrofit2.Call;
import retrofit2.Response;

//我的钱包
public class MywalletActivity extends Activity implements View.OnClickListener {

    private ImageView mywallet_back;

    private LinearLayout mywallet_linear1, mywallet_linear2, mywallet_linear3;

    private TextView mywallet_txt1, mywallet_txt2;

    private GridView mywallet_gridview;
    List<GridviewBean> data;
    GridviewBean bean;
    GridViewAdapter gridViewAdapter;

    //图片数据
    int[] images = {R.drawable.mywallet_pic1, R.drawable.mywallet_pic2, R.drawable.mywallet_pic3,
            R.drawable.mywallet_pic4, R.drawable.mywallet_pic5, R.drawable.mywallet_pic6};
    //图片编号
    String[] name = {"手机充值", "信用卡还款", "生活缴费", "违章查询", "票务预订", "酒店预订"};

    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mywallet);

        TobuyApplication.getmInstance().onActivityCreate(this);

        sp = getSharedPreferences("ToBuy", Context.MODE_PRIVATE);

//        mywallet_txt1 = findViewById(R.id.mywallet_txt1);
//        mywallet_txt2 = findViewById(R.id.mywallet_txt2);
//        mywallet_linear1 = findViewById(R.id.mywallet_linear1);
//        mywallet_linear2 = findViewById(R.id.mywallet_linear2);
//        mywallet_linear3 = findViewById(R.id.mywallet_linear3);
//        mywallet_back = findViewById(R.id.mywallet_back);
//        mywallet_gridview = findViewById(R.id.mywallet_gridview);
//        mywallet_linear1.setOnClickListener(this);
//        mywallet_linear2.setOnClickListener(this);
//        mywallet_linear3.setOnClickListener(this);
//        mywallet_back.setOnClickListener(this);
//
//        data = new ArrayList<>();
//
//        //初始化数据
//        for (int i = 0; i < images.length; i++) {
//            bean = new GridviewBean(images[i], name[i]);
//            data.add(bean);
//        }
//
//        gridViewAdapter = new GridViewAdapter(data, MywalletActivity.this);
//        mywallet_gridview.setAdapter(gridViewAdapter);
    }


    @Override
    protected void onResume() {
        super.onResume();
        GetBankcardParamBean g = new GetBankcardParamBean();
        g.setCustomId(sp.getString("id", "0"));
        Call<ReturnofincomeBean> userMsgCall = HelloWordModel.getInstance(this).getReturnofincome(SystemDatas.GetService_URL("getreturn"), g);//, sp.getString("token", null)
        userMsgCall.enqueue(new retrofit2.Callback<ReturnofincomeBean>() {
            @Override
            public void onResponse(Call<ReturnofincomeBean> call, final Response<ReturnofincomeBean> response) {
                try {
                    if (response.body().getMsg().equals("success")) {
                        String formatMoney = StringUtil.getFormatPrice(response.body().getData().getCustom().getCurrentMoney());
                        mywallet_txt1.setText(formatMoney);
                        mywallet_txt2.setText("0");
                    } else {
                        Toast.makeText(MywalletActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ReturnofincomeBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.mywallet_back:
//                finish();
//                break;
//            case R.id.mywallet_linear1:
//                //TODO
//                Intent intent = new Intent(MywalletActivity.this, BankCardAct.class);
//                startActivity(intent);
//                break;
//            case R.id.mywallet_linear2:
//                intent = new Intent(MywalletActivity.this, ReturnofincomeActivity.class);
//                startActivity(intent);
//                break;
//            case R.id.mywallet_linear3:
//                Toast.makeText(MywalletActivity.this, "功能正在建设中!", Toast.LENGTH_SHORT).show();
////                intent = new Intent();
////                startActivity(intent);
//                break;
        }

    }

    public class GridViewAdapter extends BaseAdapter {

        private List<GridviewBean> data;//数据

        private Context context;//上下文

        public GridViewAdapter(List<GridviewBean> data, Context context) {
            this.data = data;
            this.context = context;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            ViewHolder viewHolder;
            if (convertView == null) {
                view = LayoutInflater.from(context).inflate(R.layout.gridview_item1, null, false);
                viewHolder = new ViewHolder();
                viewHolder.gridview_linear = (LinearLayout) view.findViewById(R.id.gridview_linear);
                viewHolder.imageView = (ImageView) view.findViewById(R.id.girdview_img);
                viewHolder.textView = (TextView) view.findViewById(R.id.gridview_text);
                view.setTag(viewHolder);
            } else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.imageView.setImageResource(data.get(position).getBitmapId());
            viewHolder.textView.setText(data.get(position).getName());

            viewHolder.gridview_linear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Toast.makeText(MywalletActivity.this, "暂无此功能!", Toast.LENGTH_SHORT).show();

//                    Intent intent = null;
//                    if (position == 0) {
//                        intent = new Intent(context, PickupmachineActivity.class);
//                    } else if (position == 1) {
//                        intent = new Intent(context, BusinessmanagementActivity.class);
//                    } else if (position == 2) {
//                        intent = new Intent(context, ShareActivity.class);
//                    } else if (position == 3) {
//                        intent = new Intent(context, MyEarningsActivity.class);
//                    } else if (position == 4) {
//                        intent = new Intent(context, KnowledgelecturehallActivity.class);
//                    } else if (position == 5) {
//                        intent = new Intent(context, EnterpriseassistanceActivity.class);
//                    }
//                    context.startActivity(intent);
                }
            });
            return view;
        }

        private class ViewHolder {
            LinearLayout gridview_linear;
            ImageView imageView;
            TextView textView;
        }

    }


    @Override
    protected void onPause() {
        super.onPause();
    }

}
